<?php
/**
 * 
 * IndexAction.class.php (前台首页)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class ShareAction extends BaseAction
{
    function _initialize()
    {	
	parent::_initialize();
    }
    
    public function index()
    {
        $this->Urlrule =F('Urlrule');
	$catid =  isset($_REQUEST['id'])?intval($_REQUEST['id']):142;
	$p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
            $module="Share";
            
        if($catid){
            $cat = $this->categorys[$catid];
            $bcid = explode(",",$cat['arrparentid']); 
            $bcid = $bcid[1]; 
            if($bcid == '') $bcid=intval($catid);
            unset($cat['id']);
            $this->assign($cat);
            $cat['id']=$catid;
            $this->assign('catid',$catid);
            $this->assign('bcid',$bcid);
        }
	$fields = F($module.'_Field');
        foreach($fields as $key=>$r){
            $fields[$key]['setup'] =string2array($fields[$key]['setup']);
        }
	$this->assign ( 'fields', $fields); 

        
        $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
        $this->assign ('seo_title',$seo_title);
        $this->assign ('seo_keywords',$cat['keywords']);
        $this->assign ('seo_description',$cat['description']);

        //if(!$catid) $catid=27;
        
        if(empty($cat['listtype'])){
            $where = " status=1 ";
            if($cat['child']){							
                    $where .= " and catid in(".$cat['arrchildid'].")";			
            }else{
                    $where .=  " and catid=".$catid;			
            }
            
            $act =  !empty($_REQUEST['act'])?$_REQUEST['act']:"new";
            $orderstr = "createtime desc,id desc";
            if($act=="hot"){
                $orderstr = "hits desc,createtime desc,id desc";
            }
            elseif($act=="like"){
                $orderstr = "like desc,createtime desc,id desc";
            }
            else{
                $orderstr = "createtime desc,id desc";
            }
            $this->dao= M($module);
            $count = $this->dao->where($where)->count();
            if($count){
                    import ( "@.ORG.Page" );
                    $listRows =  32;
                    $page = new Page ( $count, $listRows );
                    $pages = $page->preshow();
                    $field =  $this->module[$this->mod[$module]]['listfields'];
                    $field =  "*";
                    $list = $this->dao->field($field)->where($where)->order($orderstr)->limit($page->firstRow . ',' . $page->listRows)->select();
                    $this->assign('pages',$pages);
                    $this->assign('list',$list);
            }
            $template_r = 'list';
        }
        $this->assign('act',$act);
        $this->assign('module_name',$module);
        $template = $cat['template_list'] ? $cat['template_list'] : $template_r;
        $this->display($module.':'.$template);
		
    }
 
    public function show($id='',$module='')
    {
        $this->Urlrule =F('Urlrule');
        $p= max(intval($_REQUEST[C('VAR_PAGE')]),1);		
        $id = $id ? $id : intval($_REQUEST['id']);
        $module="Share";
        $this->assign('module_name',$module);
        $this->dao= M($module);
        $data = $this->dao->find($id);

        $catid = $data['catid'];
        $cat = $this->categorys[$data['catid']];
        if(empty($cat['ishtml']))$this->dao->where("id=".$id)->setInc('hits'); //添加点击次数
        $bcid = explode(",",$cat['arrparentid']); 
        $bcid = $bcid[1]; 
        if($bcid == '') $bcid=intval($catid);

        $seo_title = $data['title'].'-'.$cat['catname'];
        $this->assign ('seo_title',$seo_title);
        $this->assign ('seo_keywords',$data['keywords']);
        $this->assign ('seo_description',$data['description']);
        $this->assign ( 'fields', F($cat['moduleid'].'_Field') ); 


        $fields = F($this->mod[$module].'_Field');
        foreach($data as $key=>$c_d){
                $setup='';
                $fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);
                if($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text'){
                        $data[$key.'_old_val'] =$data[$key];
                        $data[$key]=fieldoption($fields[$key],$data[$key]);
                }elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){ 
                        if(!empty($data[$key])){
                                $p_data=explode(':::',$data[$key]);
                                $data[$key]=array();
                                foreach($p_data as $k=>$res){
                                        $p_data_arr=explode('|',$res);					
                                        $data[$key][$k]['filepath'] = $p_data_arr[0];
                                        $data[$key][$k]['filename'] = $p_data_arr[1];
                                }
                                unset($p_data);
                                unset($p_data_arr);
                        }
                }
                unset($setup);
        }
        $this->assign('fields',$fields); 


        //手动分页
        $CONTENT_POS = strpos($data['content'], '[page]');
        if($CONTENT_POS !== false) {
                $urlrule = geturl($cat,$data,$this->Urlrule);
                $urlrule =  str_replace('%7B%24page%7D','{$page}',$urlrule); 
                $contents = array_filter(explode('[page]',$data['content']));
                $pagenumber = count($contents);
                for($i=1; $i<=$pagenumber; $i++) {
                        $pageurls[$i] = str_replace('{$page}',$i,$urlrule);
                } 
                $pages = content_pages($pagenumber,$p, $pageurls);
                //判断[page]出现的位置是否在文章开始
                if($CONTENT_POS<7) {
                        $data['content'] = $contents[$p];
                } else {
                        $data['content'] = $contents[$p-1];
                }
                $this->assign ('pages',$pages);	
        }
        if($data["proid"]>0){
            $pro=M("Product")->find($data["proid"]);
            $this->assign ('pro',$pro);
        }
        else{
            $promsg="很快就有商品哟!";
            $this->assign ('promsg',$promsg);
        }
        $userbuynum=M("Order")->where("userid=".$data["userid"])->count();
        $usersharenum=M("Share")->where("userid=".$data["userid"])->count();
        $this->assign('userbuynum',$userbuynum);
        $this->assign('usersharenum',$usersharenum);
        
        $this->assign('catid',$catid);
        $this->assign ($cat);
        $this->assign('bcid',$bcid);
        $this->assign ($data);
        

        //评论
        $jgcommentcount=M("Comment")->where("contype='".MODULE_NAME."' and conid=".$id)->count();
        $this->assign('jgcommentcount',$jgcommentcount);
        
        if(!empty($data['template'])){
                $template = $data['template'];
        }elseif(!empty($cat['template_show'])){
                $template = $cat['template_show'];
        }else{
                $template =  'show';
        }       

        $this->display($module.':'.$template); 
    }
}
?>